//
//  SEGActicityTaskDetailViewController.m
//  Commom
//
//  Created by Clavis on 2020/7/31.
//

#import "SEGActicityTaskDetailViewController.h"
#import "SEGActivityTaskDetailCell.h"

@interface SEGActicityTaskDetailViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *priceTipLabel;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UILabel *endDateLabel;
@property (nonatomic, strong) UILabel *taskDesLabel;
@property (nonatomic, strong) UIView *tableFooterView;

@end

@implementation SEGActicityTaskDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatUI];
    [self updateUI];
}

- (void)creatUI {
    self.view.backgroundColor = kColor_bg3;
    UIView *contanerView = [[UIView alloc] init];
    contanerView.backgroundColor = kColor_W2;
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, LYScreenWidth(), LYScreenHeight()*0.7) byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(15,15)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = CGRectMake(0, 0, LYScreenWidth(), LYScreenHeight()*0.7);
    maskLayer.path = maskPath.CGPath;
    contanerView.layer.mask = maskLayer;
    [self.view addSubview:contanerView];
    [contanerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(LYScreenHeight()*0.7);
    }];
    
    UIButton *tapButton = ({
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.view addSubview:button];
        button;
    });
    [tapButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(0);
        make.bottom.equalTo(contanerView.mas_top);
    }];
    @weakify(self);
    [[tapButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = UIColor.clearColor;
    [contanerView addSubview:headerView];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(0);
        make.height.mas_equalTo(LYScreenWidth()*120/750.0);
    }];
    
    UIImageView *headerImageView = ({
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"pic_fuli_Popup_top")];
        [headerView addSubview:imageView];
        imageView;
    });
    [headerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    self.titleLabel = ({
        UILabel *label = [[UILabel alloc] init];
        label.text = @"新手任务";
        label.textColor = kColor_W;
        label.font = [UIFont boldSystemFontOfSize:17];
        label.textAlignment = NSTextAlignmentCenter;
        [headerView addSubview:label];
        label;
    });
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(22);
        make.left.mas_equalTo(53);
        make.right.mas_equalTo(-53);
        make.centerX.mas_offset(0);
    }];
    
    self.priceTipLabel = ({
        UILabel *label = [[UILabel alloc] init];
        label.numberOfLines = 0;
        label.text = @"奖";
        label.textColor = UIColor.whiteColor;
        label.font = kFont_13;
        label.backgroundColor = kColor_S1;
        label.textAlignment = NSTextAlignmentCenter;
        [headerView addSubview:label];
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 23, 21) byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(3,3)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = CGRectMake(0, 0, 23, 21);
        maskLayer.path = maskPath.CGPath;
        label.layer.mask = maskLayer;
        label;
    });
    [self.priceTipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(15);
        make.width.mas_equalTo(23);
        make.height.mas_equalTo(21);
    }];
    
    UIButton *closeButton = ({
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_fuli_delete")] forState:UIControlStateNormal];
        [button setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_fuli_delete")] forState:UIControlStateHighlighted];
        [headerView addSubview:button];
        button;
    });
    [closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(30);
        make.top.mas_equalTo(6);
        make.right.mas_equalTo(-6);
    }];
    [[closeButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    
    [contanerView addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.equalTo(headerView.mas_bottom);
    }];
    
    UIView *tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, LYScreenWidth(), 45)];
    self.endDateLabel = ({
        UILabel *label = [[UILabel alloc] init];
        label.numberOfLines = 0;
        label.text = @"07-12结束";
        label.textColor = kColor_C;
        label.font = kFont_11;
        [tableHeaderView addSubview:label];
        label;
    });
    [self.endDateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(20);
    }];
    self.tableView.tableHeaderView = tableHeaderView;
    
    self.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, LYScreenWidth(), 100)];
    self.taskDesLabel = ({
        UILabel *label = [[UILabel alloc] init];
        label.numberOfLines = 0;
        label.text = @"任务说明：每天加持打卡，赢大奖！每天加持打卡，赢大奖！每天加持打卡，赢大奖！";
        label.textColor = kColor_C;
        label.font = kFont_11;
        [self.tableFooterView addSubview:label];
        label;
    });
    [self.taskDesLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(15);
    }];
    self.tableView.tableFooterView = self.tableFooterView;
}

- (void)updateUI {
    // title & 奖-label
    self.titleLabel.text = self.taskModel.mainTaskTopic;
    self.priceTipLabel.hidden = self.taskModel.whetherShowLottery != 1;

    // endtime
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:self.taskModel.endTime.longLongValue];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd";
    self.endDateLabel.text = [NSString stringWithFormat:@"%@结束",[formatter stringFromDate:date]];
    
    // rule
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithData:[self.taskModel.mainTaskRule dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    self.taskDesLabel.attributedText = attributedString;
    CGRect desRect = [attributedString boundingRectWithSize:CGSizeMake(LYScreenWidth()-30, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin context:nil];
    self.tableFooterView.frame = CGRectMake(0, 0, LYScreenWidth(), desRect.size.height + 30);
    
    // reload
    [self.tableView reloadData];
}

#pragma mark - <UITableViewDelegate, UITableViewDataSource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.taskModel.childTaskList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SEGActivityTaskDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SEGActivityTaskDetailCell" forIndexPath:indexPath];
    [cell updateCellWithChildTaskModel:self.taskModel.childTaskList[indexPath.row]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SEGActivityChildTaskModel *childModel = self.taskModel.childTaskList[indexPath.row];
    if (self.didSelectChildTask) {
        self.didSelectChildTask(childModel);
    }
}

#pragma mark - Getter
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = UIColor.clearColor;
        [_tableView registerClass:[SEGActivityTaskDetailCell class] forCellReuseIdentifier:@"SEGActivityTaskDetailCell"];
        _tableView.contentInset = UIEdgeInsetsMake(0, 0, LYSafeAreaBottom() + 30, 0);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

@end

